package com.cn.zengzeng.spbzengzeng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.zengzeng.spbzengzeng.annotation.JwtToken;
import com.cn.zengzeng.spbzengzeng.po.ErrorInfoRecord;
import com.cn.zengzeng.spbzengzeng.test.HttpClientHelper;
import com.cn.zengzeng.spbzengzeng.vo.ActionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: zhangzengzeng
 * @Date: 2021/9/30 13:37
 */
@RestController
@RequestMapping("/weather")
@Slf4j
public class WeatherController {


    @JwtToken(adminuser = true)
    @PostMapping(value = "/getWeather")
    public ActionResult getWeather(@RequestBody JSONObject jsonObject){
        log.info("getWeather入参:{}",jsonObject.toJSONString());
        String city = jsonObject.getString("city");
        if(!jsonObject.isEmpty()){
            try {
                Map<String, String> hashMap = new HashMap<>();
                hashMap.put("Content-Type","application/json");
                String s = HttpClientHelper.doGet("http://wthrcdn.etouch.cn/weather_mini",jsonObject,hashMap);
                if(s!=null){
                    JSONObject jsonObject1 = JSON.parseObject(s);
                    if(jsonObject1.getInteger("status")==1002){
                        return new ActionResult("1002","参数异常","");
                    }
                    JSONObject data = (JSONObject) jsonObject1.get("data");
                    return new ActionResult("200","获取天气信息成功",data);
                }
            }catch(Exception e) {
                log.error("getWeather接口异常,异常信息:{}",e.getMessage());
            }
        }
        return new ActionResult("403","参数不能为空","");
    }
}
